package com.LeetCode.Array;

import org.junit.Test;

/**
 * 连续整数求和
 */
public class consecutiveNumbersSum {

    public int consecutiveNumbersSum(int N) {
        int res = 0;
        int curSum = 0;
        int l = 0;
        for(int r = 1; r<= N; r++){
            curSum += r;
            while(curSum > N){
                curSum -= l;
                l++;
            }
            if(curSum == N) res++;
        }
        return res;
    }

    public int consecutiveNumbersSum2(int N){
        N*=2;
        int sqrt = (int) Math.sqrt((double) N);
        int a = 0;
        int res = 0;
        for(int i = 1; i<=sqrt; i++){
            if(N%i == 0){
                a = N / i;
                if((a - i) % 2 == 1)
                    res++;
            }
        }
        return res;
    }

    @Test
    public void test(){
        int n = 9;
        System.out.println(consecutiveNumbersSum(15));
    }
}
